package com.vito.dcs.asphalt.device.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.vito.dcs.asphalt.pore.service.MixAsphaltPoreService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.exception.ParamErrorException;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.tools.I18nResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.asphalt.device.model.MixAsphaltBlenderModel;
import com.vito.dcs.asphalt.device.service.MixAsphaltBlenderService;

/**
 * 沥青搅拌机设备设置表请求控制器
 * @author  lyhao E-mail: 
 * @version 0.0.1
 * @date    2019-4-15 11:01:21
 */
@Controller
@RequestMapping("/dcs/asphalt/device")
public class MixAsphaltBlenderAction extends BaseAction {
	
	private static final Logger log = LoggerFactory.getLogger(MixAsphaltBlenderAction.class);
	
	@Reference
	private MixAsphaltBlenderService mixasphaltblenderServiceImpl;

	@Reference
	private MixAsphaltPoreService mixAsphaltPoreServiceImpl;
	
	private static volatile String id = "";
	
	
	/**
	 * 保存沥青搅拌机设备设置表
	 * @author  lyhao E-mail: 
	 * @version 创建时间：2019-4-15 11:01:21
	 * @return  code:状态码 ,msg:提示信息
	 * @param   map 需要保存的沥青搅拌机设备设置表对象
	*			3434
	 *		 	<li>StationId:沥青拌合站ID 不能为null 或空字符串</li>
	 *		 	<li>FormulaName:配方名称 不能为null 或空字符串</li>
	 *		 	<li>LayerId:结构层ID 不能为null 或空字符串</li>
	 *		 	<li>MaterialId:混合料类型ID 不能为null 或空字符串</li>
	 */
	@RequestMapping("saveMixAsphaltBlender")
	@ResponseBody
	public Result saveMixAsphaltBlender(@RequestParam Map<String,Object> map){
		Result res = new Result();
		try{
			UserModel user = SessionMap.getObj();
	        if(user == null || Validata.isNullOrEmpty(user.getUserId())){
	            res.setCode(Result.CODE_LOGIN_REQUIRE);
	            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
	            return res;
	        }
	        
	        if(id.equals(map.get("pageTokenId"))){
				res.setCode(Result.CODE_OK);
				return res;
			}
	        
			map.put("updateUser", user.getUserId());
			id = (String) map.get("pageTokenId");
			mixasphaltblenderServiceImpl.saveMixAsphaltBlender(map);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.SAVESUC);
		}catch(Exception ex){
			log.error("保存失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 分页查询沥青搅拌机设备设置表
	 * @author  lyhao E-mail: 
	 * @version 创建时间：2019-4-15 11:01:21
	 * @return  {code:0,data:{total:20,rows:[{}...]}}
	 * @param   mixasphaltblender 依靠沥青搅拌机设备设置表对象分页查询
	 *          <li>pageNo default 1</li>
	 *          <li>pageSize default 15</li>
	 */
	@RequestMapping("queryMixAsphaltBlenderByPage")
	@ResponseBody
	public Result queryMixAsphaltBlenderByPage(MixAsphaltBlenderModel mixasphaltblender){
		Result res = new Result();
		try{
			Validata.isNullOrEmpty(mixasphaltblender, "沥青搅拌机设备设置表不能为空");
			res.setData(mixasphaltblenderServiceImpl.queryMixAsphaltBlenderByPage(mixasphaltblender));
			res.setCode(Result.CODE_OK);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 查询一条沥青搅拌机设备设置表
	 * @author  lyhao E-mail: 
	 * @version 创建时间：2019-4-15 11:01:21
	 * @return  {code:0,data:{...}}
	 * @param   mixasphaltblender 依靠沥青搅拌机设备设置表对象查询
	 */
	@RequestMapping("queryMixAsphaltBlender")
	@ResponseBody
	public Result queryMixAsphaltBlender(MixAsphaltBlenderModel mixasphaltblender){
		Result res = new Result();
		try{
			res.setData(mixasphaltblenderServiceImpl.queryMixAsphaltBlender(mixasphaltblender));
			res.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
		
	/**
	 * 更新沥青搅拌机设备设置表对象
	 * @author  lyhao E-mail: 
	 * @version 创建时间：2019-4-15 11:01:21
	 * @return  {code:0,data:10}
	 * @param   map 依靠沥青搅拌机设备设置表对象的id变更传入的其他属性值
	 *		 	<li>Id:主键ID 不能为null 或空字符串</li>
	 *		 	<li>StationId:沥青拌合站ID 不能为null 或空字符串</li>
	 *		 	<li>FormulaName:配方名称 不能为null 或空字符串</li>
	 *		 	<li>LayerId:结构层ID 不能为null 或空字符串</li>
	 *		 	<li>MaterialId:混合料类型ID 不能为null 或空字符串</li>
	 */
	@RequestMapping("updateMixAsphaltBlender")
	@ResponseBody
	public Result updateMixAsphaltBlender(@RequestParam Map<String,Object> map){
		Result res = new Result();
		try{
			UserModel user = SessionMap.getObj();
	        if(user == null || Validata.isNullOrEmpty(user.getUserId())){
	            res.setCode(Result.CODE_LOGIN_REQUIRE);
	            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
	            return res;
	        }
	        
	        if(id.equals(map.get("pageTokenId"))){
				res.setCode(Result.CODE_OK);
				return res;
			}
	        
			map.put("updateUser", user.getUserId());
			
			id = (String) map.get("pageTokenId");
			
			mixasphaltblenderServiceImpl.updateMixAsphaltBlender(map);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.UPDSUC);
		}catch(Exception ex){
			log.error("更新失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 删除沥青搅拌机设备设置表对象
	 * @author  lyhao E-mail: 
	 * @version 创建时间：2019-4-15 11:01:21
	 * @return  {code:0,data:10}
	 * @param   mixasphaltblender 删除沥青搅拌机设备设置表对象的 checkeds 标识的id对应的数据
	 */
	@RequestMapping("deleteMixAsphaltBlender")
	@ResponseBody
	public Result deleteMixAsphaltBlender(MixAsphaltBlenderModel mixasphaltblender){
		Result res = new Result();
		try{
			mixasphaltblenderServiceImpl.deleteMixAsphaltBlender(mixasphaltblender);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("删除失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	@RequestMapping("updateIsCurrent")
	@ResponseBody
	public Result updateIsCurrent(@RequestParam Map<String,Object> map){
		Result rs = new Result();
		try{
			mixasphaltblenderServiceImpl.updateIsCurrent(map);
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("修改成功",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	@RequestMapping("findBlenderBySectionId")
	@ResponseBody
	public Result findBlenderBySectionId(@RequestParam Map<String,String> map){
		Result rs = new Result();
		try{
			rs.setData(mixasphaltblenderServiceImpl.findBlenderBySectionId(map));
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("修改成功",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}

	/**
	 * 新增接口根據拌合站ID查詢是否存在攪拌機
	 * @return
	 */
	@RequestMapping("queryAsphaltStationDel")
	@ResponseBody
	public Result queryAsphaltStationDel(String blenderId){
		Result rs = new Result();
		try{
		    if(blenderId == null){
                blenderId = "0";
            }
		   /* List<String> list = new ArrayList<String>();
		    String [] arg = blenderId.split(",");
            for (int i = 0; i < arg.length; i++) {
                list.add(arg[i].toString());
            }*/
			rs.setData(mixAsphaltPoreServiceImpl.queryAsphaltPoreBlenderId(blenderId));
			rs.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}

	/**
	 * 根据工艺ID 和 搅拌机编号获取是否存在记录
	 * @param map
	 * @return
	 */
	@RequestMapping("queryAsphaltLayerIdMachineNo")
	@ResponseBody
	public Result queryAsphaltLayerIdMachineNo(@RequestParam Map<String,String> map){
		Result rs = new Result();
		try{
			rs.setData(mixasphaltblenderServiceImpl.queryAsphaltLayerIdMachineNo(map));
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("查询失败",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
}



